#!/usr/bin/env bash

python_version="python2.7"
venv_dir="python_venv"

if [[ ! $(which virtualenv) ]]; then
  echo ""
  echo "### Couldn't find 'virtualenv' command, trying to install it..."
  echo ""
  sudo yum install -y epel-release && \
  sudo yum install -y python-virtualenv libselinux-python || \
  sudo apt-get install -y virtualenv
fi

if [[ ! $(which pip) ]]; then
  echo ""
  echo "### Couldn't find 'pip' command, trying to install it..."
  echo ""
  wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py || curl -SL https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
  sudo python /tmp/get-pip.py -i https://pypi.doubanio.com/simple
fi

echo ""
echo "### Creating virtualenv virtual environment..."
echo ""
virtualenv --clear python_venv

echo ""
echo "### Copy /usr/lib64/$python_version/site-packages/selinux ./$venv_dir/lib65/python/site-packages/"
echo ""
cp -R /usr/lib64/$python_version/site-packages/selinux ./$venv_dir/lib64/$python_version/site-packages/

echo ""
echo "### Activating virtualenv..."
echo ""
source ./$venv_dir/bin/activate

echo ""
echo "### Installing python packages..."
echo ""
pip install -i https://pypi.doubanio.com/simple wheel
pip install -i https://pypi.doubanio.com/simple -r ./kubespray/requirements.txt

if [[ $? -eq 0 ]]; then
  echo "### Python virtual environment is ready."
  echo ""
  echo "#######################################################################"
  echo "### Please run 'source ./$venv_dir/bin/activate' to activate it."
  echo "### 请运行 'source ./$venv_dir/bin/activate' 激活 Python 独立环境."
  echo "#######################################################################"
fi
